﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Boxie
{
    class BoxieMusicLib
    {
        public static String ReadTagData(String fp)
        {
            try
            {
                String ns = fp.Substring(0, fp.LastIndexOf("\\"));
                String fn = System.IO.Path.GetFileName(fp);
                Shell32.Shell shell = new Shell32.Shell();
                Shell32.Folder folder = shell.NameSpace(ns);
                Shell32.FolderItem folderItem = folder.ParseName(fn);
                if (folderItem != null)
                {
                    String t = "%ext% = " + System.IO.Path.GetExtension(fp).Substring(1).ToLower() + Environment.NewLine;
                    for (int x = 0; x <= 40; x++)
                        t += "%" + x.ToString() + "% = " + folder.GetDetailsOf(folderItem, x) + Environment.NewLine;

                    return t;
                }
                return "No data available";
            }
            catch (Exception e)
            {
                return "No data available";
            }
            
        }

        public static String ReadSpecificTag(string fp, int id)
        {
            try
            {
                String ns = fp.Substring(0, fp.LastIndexOf("\\"));
                String fn = System.IO.Path.GetFileName(fp);
                Shell32.Shell shell = new Shell32.Shell();
                Shell32.Folder folder = shell.NameSpace(ns);
                Shell32.FolderItem folderItem = folder.ParseName(fn);
                if (folderItem != null)
                    return folder.GetDetailsOf(folderItem, id);
                return "Error";
            }
            catch (Exception e)
            {
                return "Error";
            }
        }

        public static bool DoTagging(string[] aOld, string fileFormat)
        {
            bool mov = BoxieRegLib.GetSettingBool("Auto/AutoMove", false);
            string movl = BoxieRegLib.GetSettingString("Auto/MoveLocation", "");
            int c = aOld.Length - 1;
            string[] aNew = new string[c+1];

            for (int x = 0; x <= c; x++)
            {
                string path = aOld[x].Substring(0, aOld[x].LastIndexOf("\\"));
                string fn = fileFormat;
                fn = fn.Replace("%ext%", System.IO.Path.GetExtension(aOld[x]).Substring(1).ToLower());
                for (int i = 0; i <= 40; i++)
                    fn = fn.Replace("%" + i.ToString() + "%", Boxie.BoxieMusicLib.ReadSpecificTag(aOld[x], i));
                aNew[x] = path + "\\" + fn;
            }
            String mb = "The following changes will be made to your system:\n\n";
            for (int x = 0; x <= c; x++)
                mb += System.IO.Path.GetFileName(aOld[x]) + " => " + System.IO.Path.GetFileName(aNew[x]) + "\n";
            if (mov)
                mb += "\nThe files will be moved to: " + movl + "\n";
            mb += "\nDo you want to make these changes now?";
            if (System.Windows.Forms.MessageBox.Show(mb, "Confirm?", System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
            {
                for (int x = 0; x <= c; x++)
                {
                    if(mov)
                        System.IO.File.Move(aOld[x], movl + "\\" + System.IO.Path.GetFileName(aNew[x]));
                    else
                        System.IO.File.Move(aOld[x], aNew[x]);
                }
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
